package pers.zcr.meringue.monolithic.system.captcha.gatewayImpl.hutoolcaptcha.config;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.zcr.meringue.monolithic.system.captcha.gatewayImpl.hutoolcaptcha.CaptchaGenerator;
import pers.zcr.meringue.monolithic.system.captcha.gatewayImpl.hutoolcaptcha.CharCaptchaGenerator;
import pers.zcr.meringue.monolithic.system.captcha.gatewayImpl.hutoolcaptcha.MathCaptchaGenerator;
import pers.zcr.meringue.monolithic.system.captcha.config.properties.CaptchaProperties;

import java.awt.*;

/**
 * 验证码配置
 *
 * @author Lion Li
 */
@Configuration
@EnableConfigurationProperties(CaptchaProperties.class)
public class CaptchaConfig {

    private static final int WIDTH = 160;
    private static final int HEIGHT = 60;
    private static final Color BACKGROUND = Color.PINK;
    private static final Font FONT = new Font("Arial", Font.BOLD, 48);

    /**
     * 圆圈干扰验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "category", havingValue = "CIRCLE",matchIfMissing = true)
    public AbstractCaptcha circleCaptcha(CodeGenerator codeGenerator) {
        AbstractCaptcha captcha = CaptchaUtil.createCircleCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        captcha.setGenerator(codeGenerator);
        return captcha;
    }

    /**
     * 线段干扰的验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "category", havingValue = "LINE")
    public AbstractCaptcha lineCaptcha(CodeGenerator codeGenerator) {
        AbstractCaptcha captcha = CaptchaUtil.createLineCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        captcha.setGenerator(codeGenerator);
        return captcha;
    }

    /**
     * 扭曲干扰验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "category", havingValue = "SHEAR")
    public AbstractCaptcha shearCaptcha(CodeGenerator codeGenerator) {
        AbstractCaptcha captcha = CaptchaUtil.createShearCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        captcha.setGenerator(codeGenerator);
        return captcha;
    }

    /**
     * huTool数字验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "type", havingValue = "MATH")
    public CodeGenerator mathCodeGenerator(CaptchaProperties captchaProperties) {
        return new MathGenerator(captchaProperties.getNumberLength());
    }

    /**
     * 数字验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "type", havingValue = "MATH")
    public CaptchaGenerator mathCaptchaGenerator(AbstractCaptcha abstractCaptcha) {
        return new MathCaptchaGenerator(abstractCaptcha);
    }

    /**
     * huTool字符串验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "type", havingValue = "CHAR",matchIfMissing = true)
    public CodeGenerator charCodeGenerator(CaptchaProperties captchaProperties) {
        return new RandomGenerator(captchaProperties.getCharLength());
    }


    /**
     * 数字验证码
     */
    @Bean
    @ConditionalOnProperty(prefix = "captcha",name = "type", havingValue = "CHAR",matchIfMissing = true)
    public CaptchaGenerator charCaptchaGenerator(AbstractCaptcha abstractCaptcha) {
        return new CharCaptchaGenerator(abstractCaptcha);
    }

}
